//
//  EJSInputValiCodeViewController.m
//  EnjoyShoppingStreet
//
//  Created by lifayu on 2018/1/8.
//  Copyright © 2018年 lifayu. All rights reserved.
//

#import "EJSInputValiCodeViewController.h"
#import <UIView+BlocksKit.h>
#import "UIPlaceHolderTextView.h"
#import "EJSGetPhoneCodeRequest.h"
#import "EJSLoginRequest.h"
#import "EJSShoppingCarSingleton.h"

@interface EJSInputValiCodeViewController ()<UITextFieldDelegate>

@property (strong, nonatomic) IBOutletCollection(UILabel) NSArray *codeLabels;
@property (weak, nonatomic) IBOutlet UITextField *codelLabelText;
@property (weak, nonatomic) IBOutlet UIView *inputCodeView;
@property (weak, nonatomic) IBOutlet UILabel *tipLabel;
@property (weak, nonatomic) IBOutlet UIButton *sendCodeButton;


@end

@implementation EJSInputValiCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)configOwnViews{
    [self.inputCodeView bk_whenTapped:^{
        [self.codelLabelText becomeFirstResponder];
    }];
    
    [self.codelLabelText addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    self.tipLabel.text = [NSString stringWithFormat:@"已向 %@ %@ 发送验证码",[EJSUserModel countryCode],self.phone];
    [self.sendCodeButton cf_startWithTime:60 title:@"重新发送" countDownTitle:@"秒后重新获取"];
    [self.codelLabelText becomeFirstResponder];
}

#pragma mark - Actions

- (IBAction)backAction:(id)sender {
    [self leftBarButtonAction:nil];
}
- (IBAction)valiCodeAction:(UIButton *)sender {
    FlyWeakSelf
    EJSGetPhoneCodeRequest *req = [EJSGetPhoneCodeRequest new];
    req.phone = self.phone;
    req.countryCode = [EJSUserModel countryCode];
    [req asyncRequestWithsuccessHandler:^(BaseResponse *response) {
        [weakSelf.sendCodeButton cf_startWithTime:60 title:@"重新发送" countDownTitle:@"秒后重新获取"];
    } failHandler:^(BaseResponse *response) {
        [[HUDHelper sharedInstance] tipMessage:response.message];
    }];
}
- (IBAction)loginAction:(id)sender {
    NSString *code = self.codelLabelText.text;
    if(!code || code.length != 6){
        [[HUDHelper sharedInstance] tipMessage:@"请输入正确的验证码"];
        return;
    }
    EJSLoginRequest *req = [EJSLoginRequest new];
    req.code = code;
    req.phone = _phone;
    req.countryCode = [EJSUserModel countryCode];
    [req asyncRequestWithsuccessHandler:^(BaseResponse *response) {
        EJSUserTokenModel *tokenModel = response.data;
        [tokenModel save];
        
        [self dismissViewControllerAnimated:NO completion:nil];
        [self.presentingViewController dismissViewControllerAnimated:NO completion:^{
            [[EJSShoppingCarSingleton sharedSingleton]requestShoppingList];
        }];
    } failHandler:^(BaseResponse *response) {
        [[HUDHelper sharedInstance] tipMessage:response.message];
    }];
}


- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSLog(@"变化%@", string);
    if([string isEqualToString:@"\n"]) {
        //按回车关闭键盘
        [textField resignFirstResponder];
        return NO;
    } else if(string.length == 0) {
        //判断是不是删除键
        return YES;
    }
    else if(textField.text.length >= 6) {
        //输入的字符个数大于6，则无法继续输入，返回NO表示禁止输入
        NSLog(@"输入的字符个数大于6，忽略输入");
        return NO;
    } else {
        return YES;
    }
}

/**
 *  重置显示的点
 */
- (void)textFieldDidChange:(UITextField *)textField
{
    NSLog(@"%@", textField.text);
    for (UILabel *textLabel in self.codeLabels) {
        textLabel.text = @"";
    }
    for (int i = 0; i < textField.text.length; i++) {
        UILabel *textLabel = [self.codeLabels objectAtIndex:i];
        textLabel.text = [textField.text substringWithRange:NSMakeRange(i, 1)];
    }
    
    if (textField.text.length == 6) {
        NSLog(@"输入完毕");
    }
}


@end
